//medicine_menu开头
$(function(){
	$(".right_ul li a").css("float","left");
	$(".right_ul li a").eq(0).css({"border":"none","paddingRight":0,"color":"#e55251"})
	$(".right_ul li:last>a").css("border","none");
	$(".right_ul li:last>span").css({"float":"left","margin":"0 -5px 0 5px","color":"#e54948"});
	$(".right_ul li:eq(4)>a").css({"color":"#cbaf82","font-weight":"800"})	
})
//子页subpage搜索框
$seTxt = $(".medicine_search #txt");
$oUl = $(".medicine_search .ul1");
$seTxt.focus(function(){
	$(this).val("");
});
$seTxt.keyup(function(){
	var $str = $(this).val();
	$oUl.html("");
	$oUl.css("display","block");
//	$("<script src=https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=" + $str + "&cb=fn").appendTo("head:eq(0)");
	$(("<script>"),{
		src:"https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=" + $str + "&cb=fn" 
	}).appendTo("head:eq(0)");
});
function fn(data){
	data.s.forEach(function(value,index){
		let oLi = document.createElement("li");
		oLi.innerHTML = value;
		$oUl.append(oLi);
	})
}
/*+++++++++++++++++++++二级菜单++++++++++++++++++++*/
//二级菜单划出
$(".nav_ul2 li:eq(0)").hover(function(){
	$(".nav_ul1").css("z-index",3)
},function(){
	$(".nav_ul1").css("z-index","-1")
})
//三级菜单
$(".nav_ul1 li").hover(function(){
	$(this).animate({"margin-left":"5px"},300);
	$(this).children("a").css({"color":"#e96875"});
},function(){
	$(this).animate({"margin-left":0},300);
	$(this).children("a").css({"color":"#f1f1f1"});
})
/*+++++++++++++++++++++列表页主要内容 container++++++++++++++++++++*/
$.getJSON("../JSON/subpage.json",function(json){
	for(var id in json){
		$(".subpage_container").append($("<img src='" + json[id].title + "'/>"));
		$(json[id].context).each(function(index,value){
			var str = `<dl class="shoppings">
							<dt><img src="`+ value.pic +`"/></dt>
							<dd>
								<p><a href="###">`+value.p1+`</a></p>
								<p>药网价<b>`+ value.price +`</b></p>
								<span>立即购买</span>
							</dd>
						</dl>`
			$(".subpage_container").append(str);
		})
	}	
	var $goodsnum = $("dl dd").find("span");
	$goodsnum.each(function(index,value){
		$goodsnum.eq(index).parent().parent().attr("data-good-id","sp"+index);
		$goodsnum.eq(index).attr("class","shopping_cart")
	})
	
	tt();
})
/*+++++++++++++++++++++加入购物车++++++++++++++++++++*/
function tt(){
	loadCart();
	$(".medicine_sidebar div:eq(1)").click(function(){
		location.href = "../html/buy.html";
	})
	$(".shopping_cart").click(function(e){
		//获取商品的id（用来区分不同的商品）
		var goodId = $(this).parents(".shoppings").attr("data-good-id");
		//获取商品的介绍
		var goodName = $(this).siblings('p').find("a").html();
		//获取商品的价格
//		var re = /(\d+\.)/;
		var goodPrice = parseFloat($(this).siblings("p").find("b").html().substring(1));
		//获取商品的图片src
		
		var goodSrc = $(this).parents().siblings("dt").find("img").attr("src");
		console.log(goodSrc)
//		console.log(goodSrc);
		//如果cookie中没有信息会返回一个undefined ,我所须是一个字符串类型的数据，所以将它转成一个“”空字符串。保持数据类型一致。
		var cartStr = $.cookie("cart") ? $.cookie("cart") : "";
		console.log(cartStr)
		
		//将字符串转成对象
		var cartObj = convertCartStrToObj(cartStr);
		console.log(cartObj);
		//判断该商品是否已经在购物车中存在
		if(goodId in cartObj){
			//如果已存在，那么该商品的数量加1
			cartObj[goodId].num += 1;
		}else{
			//如果不存在，那么将新商品的信息存入
			cartObj[goodId] = {
				name : goodName,
				price : goodPrice,
				num : 1,
				src : goodSrc
			};
		}
		cartStr = convertObjToCartStr(cartObj);
		//存入cookie
		//document.cookie = "key=value"
		$.cookie("cart",cartStr,{expires : 7,path:"/"});
		
		//飞入购物车
		var cloneImg = $(this).parents().siblings("dt").find("img").clone().css({width:50,height:50});
//		console.log(cloneImg)
		cloneImg.fly({
			start : {
				top : e.clientY,
				left : e.clientX
			},
			end :{
				top : $(".buy").offset().top,
				left : $(".buy").offset().left,
				width:0,
				height:0
			},
			autoPlay : true,
			onEnd : function(){
				$(".buy").html(function(index,value){
			//"购物车（0）"
			var pattern = /(\d+)/;
			var num = parseInt(value.match(pattern)[1]);
//			console.log(num)
			return (num + 1);
		});
		cloneImg.remove();
			}
		})
	})
	//字符串转对象
	function convertCartStrToObj(cartStr){
		if(!cartStr){
			return {};
		}
		var goods = cartStr.split(":");
		var obj = {};
		for(var i = 0; i < goods.length; i ++){
			var data = goods[i].split(",");
			//以商品的id为健，商品的其他信息为值，这个值也设计为一个对象
			obj[data[0]] = {
				src : data[1],
				name : data[2],
				price : parseFloat(data[3]),
				num : parseInt(data[4]),
			}
		}
		return obj;
	}
	//对象转字符串
	function convertObjToCartStr(obj){
		var cartStr = "";
		//遍历对象
		for(var id in obj){
			if(cartStr){
				cartStr += ":";
			}
			cartStr += id + ","+ obj[id].src + ","  + obj[id].name + "," + obj[id].price + "," + obj[id].num ;
			}
			return cartStr;
	}
	//加载购物车中的信息（使商品页与购物车页中的购物车数量同步）
		function loadCart(){
			var cartStr = $.cookie("cart") ? $.cookie("cart") : "";
				var cartObj = convertCartStrToObj(cartStr);
				//获取到购物车中所有商品的数量
				var total = 0;
				for(var id in cartObj){
					total += cartObj[id].num;
				}
				$(".buy").html(total);
		}
}

/*+++++++++++++++++++++侧边栏回到顶部++++++++++++++++++++*/
$(".sidabar .oTop").click(function(){
	$("html,body").animate({"scrollTop":0},1000)
})

/*+++++++++++++++++++++认证、监察++++++++++++++++++++*/
function controlLi(){
	var $oControlLi = $(".medicine_control li");
//	console.log($oControlLi);
	$oControlLi.each(function(index,value){
		$(this).mouseenter(function(){
			$(this).animate({"background-position-y":"15px"},300).animate({"background-position-y":"30px"},300)
		})
	})
}
controlLi()





















